"""
全国疫情可视化地图开发
"""
import json
from pyecharts.charts import Map
from pyecharts.options import *

"""
vscode
它的相对路径是相对于当前工作根目录来定位的
也就是从工作文件夹的最顶级目录开始查找，而非是从当前执行文件开始查找。
"""

# 读取数据文件
f = open(".\\地图数据\\疫情.txt", encoding= "UTF-8")
data = f.read()

data_dict = json.loads(data)

province_data_list = data_dict["areaTree"][0]["children"] #省份数据

# 将各省数据封装到列表内
data_list = [] # 绘图需要的数据列表
for province_data_t in province_data_list:
    province_name_t = province_data_t["name"] # 省份名称要和地图中的完全一致
    # 处理一下，让它是全称
    # 自治区
    if province_name_t == "新疆":
        province_name_t += "维吾尔自治区"
    elif province_name_t == "西藏" or province_name_t == "内蒙古": # 逻辑或用 or
        province_name_t += "自治区"
    elif province_name_t == "宁夏":
        province_name_t += "回族自治区"
    elif province_name_t == "广西":
        province_name_t += "壮族自治区"
    # 直辖市
    elif province_name_t == "北京" or province_name_t == "天津" or province_name_t == "上海" or province_name_t == "重庆":
        province_name_t += "市"
    # 特别行政区
    elif province_name_t == "香港" or province_name_t == "澳门":
        province_name_t += "特别行政区"    
    # 其他省份
    else:
        province_name_t += "省"
    province_confirm = province_data_t["total"]["confirm"] # 确诊人数
    # print((province_name_t, province_confirm))
    data_list.append( ( province_name_t, province_confirm ) )


# 准备地图对象
map = Map()

# 添加数据
map.add("各省份确诊人数", data_list, "china")

# 设置全局选项
map.set_global_opts(
    title_opts= TitleOpts(title="全国疫情地图"),
    visualmap_opts=VisualMapOpts(
        is_show= True,
        is_piecewise= True,
        pieces=[
            {"min": 1, "max": 99, "lable":"1~99人", "color":"#CCFFFF"},
            {"min": 100, "max": 999, "lable":"100~9999人", "color":"#FFFF99"},
            {"min": 1000, "max": 4999, "lable":"1000~4999人", "color":"#FF9966"},
            {"min": 5000, "max": 9999, "lable":"5000~9999人", "color":"#FF6666"},
            {"min": 10000, "max": 99999, "lable":"10000~99999人", "color":"#CC3333"},
            {"min": 100000, "lable":"十万人以上", "color":"#990033"}
        ]
    ),
    toolbox_opts= ToolboxOpts(is_show=True)
)

# 绘制
map.render("全国疫情地图.html")